import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/home/HomePage.vue'
import Category from '../views/home/CategoryPage.vue'
import Message from '../views/home/MessagePage.vue'
import User from '../views/home/UserPage.vue'
import Cart from '../views/home/CartPage.vue'
import Login from '../views/login/LoginPage.vue'
import Register from '../views/login/RegisterPage.vue'
import GoodsList from '@/views/GoodsList.vue'
import GoodsDetail from '@/views/GoodsDetail.vue'
import Mypay from '@/views/MypayPage.vue'
import Setting from '@/views/SettingsPage.vue'
import PendingDelivery from '@/views/Orders/pendingDelivery.vue'
import AllOrders from '@/views/Orders/AllOrders.vue'
import OrderReview from '@/views/Orders/OrderReview.vue'
import WriteReview from '@/views/Orders/WriteReview.vue'
import CompleteOrders from '@/views/Orders/CompleteOrders.vue'
import ChatPage from '@/views/ChatPage.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home',
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
      meta:{ title: '首页', isTab: true }
    },
    {
      path: '/category',
      name: 'category',
      component: Category,
      meta:{ title: '分类', isTab: true, isShowNav: true }
    },
    {
      path: '/message',
      name: 'message',
      component: Message,
      meta:{ title: '消息', isTab: true, isShowNav: true }
    },
    {
      path: '/user',
      name: 'user',
      component: User,
      meta:{ title: '用户', isTab: true }
    },
    {
      path: '/cart',
      name: 'cart',
      component: Cart,
      meta:{ title: '购物车', isTab: true, isShowNav: true }
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: { title: '登录', isTab: true, isShowNav: true, isShowBack: true }
    },
    { 
      path: '/register', 
      component: Register, 
      name: 'register', 
      meta: { title: '注册', isTab: true, isShowNav: true, isShowBack: true } 
    },
    {
      path: '/goodslist/:category_id',
      name: 'goodslist',
      component: GoodsList,
      props: true,
      meta: { title: '商品列表', isTab: true, isShowNav: true, isShowBack: true }
    },
    {
      path: '/goodsDetail/:id',
      component: GoodsDetail, 
      props: true,
      name: 'goodsDetail', 
      meta: { title: '商品详情', isTab: false, isShowNav: true, isShowBack: true } 
    },
    {
      path: '/mypay',
      component: Mypay,
      name: 'mypay',
      meta: { title: '订单结算台', isTab: false, isShowNav: true, isShowBack: true } 
    },
    {
      path: '/setting',
      component: Setting,
      name: 'setting',
      meta: { title: '设置', isTab: false, isShowNav: true, isShowBack: true }
    },
    {
      path: '/pendingDelivery',
      component: PendingDelivery,
      name: 'pendingDelivery',
      meta: { title: '待收货', isTab: false, isShowNav: true, isShowBack: true }
    },
    {
      path: '/allorders',
      component: AllOrders,
      name: 'allorders',
      meta: { title: '全部订单', isTab: false, isShowNav: true, isShowBack: true }
    },
    {
      path: '/orderreview',
      component: OrderReview,
      name: 'orderreview',
      meta: { title: '待评价', isTab: false, isShowNav: true, isShowBack: true }
    },
    {
      path: '/review/:id',
      component: WriteReview,
      name: 'review',
      meta: { title: '评价', isTab: false, isShowNav: true, isShowBack: true }
    },
    {
      path: '/completeorders',
      component: CompleteOrders,
      name: 'completeOrders',
      meta: { title: '已完成订单', isTab: false, isShowNav: true, isShowBack: true }
    },
    {
      path: '/chatpage',
      name: 'chatpage',
      component: ChatPage,
      meta: { title: '客服', isTab: false, isShowNav: true, isShowBack:true }
    }
  ],
})

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title
  if (title) {
  document.title = title + ' - 微商城'
  }
  next() 
})

export default router
